home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Development Tools & Languages / Dylan Related / Dylan alpha demos / Online Insultant src ƒ / Sound.dylan < prev   
Encoding:
Text File  |  1994-12-13  |  3.4 KB  |  89 lines  |  [TEXT/ttxt]

  1. language: infix-dylan
  2. module: Online-Insultant
  3.  
  4. /* Copyright (C) 1994, Apple Computer, Inc. All rights reserved. */
  5.  
  6. /* This reads the insult out loud, in case the user is illiterate
  7.  *--- I should make this select a higher quality voice
  8.  *--- than the rather crummy default one
  9.  */
  10.  
  11. // Import the Speech Manager interface
  12. // I copied the file from Feb 94 Developer CD to the project directory
  13. define interface
  14.   #include "Speech.h",
  15.      name-mapper: minimal-name-mapping-with-structure-prefix,
  16.      define: {"SystemSevenOrLater"},
  17.      import: {"gestaltSpeechAttr", "gestaltSpeechMgrPresent",
  18.               "SpeakString", "SpeakText", "SpeechBusy",
  19.               "MakeVoiceSpec", "VoiceSpec",
  20.               "NewSpeechChannel", "DisposeSpeechChannel", "SpeechChannel",
  21.               "voiceNotFound",
  22.               // fun stuff for playing around, delete later
  23.               "CountVoices", "GetIndVoice",
  24.               "VoiceDescription", "GetVoiceDescription"},
  25.      type: {"StringPtr" => <Pascal-string>};
  26.   function "SpeakText", argument-type: {textBuf => <C-string>};
  27.   function "NewSpeechChannel", output-argument: chan;
  28.   function "CountVoices", output-argument: numVoices;
  29.   // We'll need Gestalt also
  30.   #include "Gestalt.h",
  31.      name-mapper: minimal-name-mapping-with-structure-prefix,
  32.      // Gestalt was not a trap in System 6
  33.      define: {"SystemSevenOrLater"},
  34.      import: {"Gestalt"};
  35.   function "Gestalt", output-argument: response;
  36.   // We need this so we can free a text-buffer
  37.   #include "memory.h",
  38.      name-mapper: minimal-name-mapping-with-structure-prefix,
  39.      define: {"SystemSevenOrLater"},          // for Gestalt
  40.      import: {"DisposePtr"};
  41. end interface;
  42.  
  43. define constant Speech-Available? = method()
  44.   let (err, result) = Gestalt($gestaltSpeechAttr);
  45.   err = 0 & logbit?($gestaltSpeechMgrPresent, result);
  46. end method;
  47.  
  48. define constant await-speech-done = method()
  49.   until ( SpeechBusy() = 0 ) end;
  50. end method;
  51.  
  52. // This compressed high-quality female voice seems to sound best
  53. define constant $preferred-voice :: <VoiceSpec> = begin
  54.                                                     let v = make(<VoiceSpec>);
  55.                                                     MakeVoiceSpec(OSType("gala"), 0, v);
  56.                                                     v;
  57.                                                   end;
  58.  
  59. // returns error code, or if successfully started asynchronous speech, values(chan, text-buffer)
  60. define constant speak-string = method(string :: <string>, #key synchronous)
  61.   let (err, chan) = NewSpeechChannel($preferred-voice);
  62.   let (err, chan) = if ( err = $voiceNotFound )
  63.                       // The preferred voice does not exist, so use the default system voice
  64.                       NewSpeechChannel(as(<VoiceSpec>, 0));
  65.                       else values(err, chan);
  66.                       end if;
  67.   if (err = 0)
  68.     let text-buffer = as(<C-string>, string);   // this makes a copy in the heap
  69.     let err = SpeakText(chan, text-buffer, size(string));
  70.     if (err = 0)
  71.       if (synchronous)
  72.         finish-speak-string(chan, text-buffer);
  73.         err;
  74.       else values(chan, text-buffer);
  75.       end if;
  76.     else err;
  77.     end if;
  78.   else err;
  79.   end if;
  80. end method;
  81.  
  82. // call this with the values from an asynchronous speak-string
  83. define constant finish-speak-string = method(chan, text-buffer)
  84.   if (instance?(chan, <SpeechChannel>))
  85.     await-speech-done();
  86.     DisposeSpeechChannel(chan);
  87.     DisposePtr(as(<machine-pointer>, text-buffer));
  88.   end if;
  89. end method;